{extend name="layout:base" /}

{block name="content"}
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <button id="btnAdd" class="layui-btn layui-btn-sm layuiadmin-btn-list" data-type="add">添加横幅</button>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <table id="tbList" class="layui-table" lay-filter="tbList"></table>
            <script type="text/html" id="op">
                <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
<!--                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>-->
            </script>
        </div>
    </div>
</div>
{/block}

{block name="script"}
<script>
    layui.config({
        base: '__STATIC__/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function () {

        //表格
        var table = layui.table;

        table.render({
            elem: '#tbList',
            url: "{:url('getList')}",
            cellMinWidth: 80,
            cols: [[
                {field: 'id', type:'number',width:50},
                {field: 'title', title: '名称', edit: 'text'},
                {
                    field: 'image', title: '图片', templet: function (d) {
                        return '<image src="' + d.image + '"/>'
                    }
                },
                {fixed: 'right', title: '操作', toolbar: '#op', width: 120}
            ]],
            id: 'tbList'
        });

        // 监听工具条
        table.on('tool(tbList)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('确定删除', {icon: 3, title: '提示'}, function (index) {
                    $.post("{:url('/admin/ad/del')}", {id: data['id']}, function (result) {
                        if (result['code'] == 0) {
                            layer.msg(result['msg'], {icon: 1, time: 2000}, function () {
                                location.reload();
                            });
                        } else {
                            layer.msg(result['msg'], {icon: 3, time: 3000});
                        }
                    });
                });
            } else if (obj.event === 'edit') {
                var url = "{:url('edit')}?id=" + data['id'];
                var index = layer.open({
                    type: 2,
                    title: '修改横幅',
                    area: ['800px', '600px'],
                    shade: 0,
                    maxmin: true,
                    content: url,
                    zIndex: layer.zIndex
                });
                layer.full(index);
            }
        });
        //监听单元格编辑
        table.on('edit(tbList)', function (obj) {
            var value = obj.value; //得到修改后的值
            var data = obj.data; //得到所在行所有键值
            var field = obj.field; //得到字段
            // layer.msg('[ID: ' + data.id + '] ' + field + ' 字段更改为：' + value);

            $.post("{:url('edit')}", {id: data.id,title: value}, function (result) {
                if (result['code'] == 0) {
                    layer.msg(result['msg'], {icon: 1, time: 1000}, function () {
                        location.reload();
                    });
                } else {
                    layer.msg(result['msg'], {icon: 3, time: 3000});
                }
            });
        });

        //操作
        var $ = layui.$, active = {
            add: function () {
                var index = layer.open({
                    type: 2,
                    title: '添加横幅',
                    area: ['800px', '600px'],
                    shade: 0,
                    maxmin: true,
                    content: "{:url('add')}",
                    zIndex: layer.zIndex
                });
                layer.full(index);
            }
        };
        $("#btnAdd").on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });

</script>
{/block}